<?php

//This controller handles to model objects - Friends & Users (that is because 'friends' is indeed 'users')
//

session_start();
/**
 * Description of users
 *
 * @author bilde
 */
class users extends CI_Controller
{
    function  __construct()
    {
        parent::__construct();

        //Logged_in control
        if (!$this->session->userdata('is_logged_in'))
        {
            redirect('/welcome/index','refresh');
        }
          
    }

    //Creates a table with all users, and show whch users a followed by the 'logged_in_user'
    //Likewise it showing who of the users a friends with the 'logged_in_user'
    function index()
    {
        $data['title'] = 'See all users';
        $data['main_content'] = 'usr/all_users';        
        $data['following'] = $this->model_follows->get_following($this->session->userdata('id'));
        $data['friends'] = $this->model_friends->get_friends($this->session->userdata('id'));
        $data['results'] = $this->model_users->listAllUsers();
        $this->load->vars($data);
        $this->load->view('includes/template');
    }

    //Creates a home-page view for every user, including 'logged_in_user, if his/her id is passed as a
    //parameter. Also is updates,posts,files,bookmarks and following selected from the database to show.
    function home($id)
    {           
        $data['main_content'] = 'usr/user_home';
        $data['user'] = $this->model_users->getUserByUsername($id);
        $data['updates'] = $this->model_updates->list_updates($data['user']['id'],10);
        $data['posts'] = $this->model_posts->list_user_posts($data['user']['id'],10);
        $data['files'] = $this->model_files->list_user_files($data['user']['id'],10);
        $data['bookmarks'] = $this->model_bookmarks->list_user_bookmarks($data['user']['id'],10);
        $following = $this->model_follows->get_following($this->session->userdata('id'));
        $following[] = $this->session->userdata('id');
        $data['following'] = $following;

        $this->load->vars($data);
        $this->load->view('includes/template');
    }

    //Makes the 'logged_in_user' to follow tagget id
    function follow($id)
    {
        $myid = $this->session->userdata('id');
        $this->model_follows->follow($myid,$id);
        redirect("socialbuddy/index", 'refresh');
    }

    //Makes the 'logged_in_user' to unfollow tagget id
    function unfollow($id)
    {
        $myid = $this->session->userdata('id');
        $this->model_follows->unfollow($myid,$id);
        redirect("socialbuddy/index", 'refresh');
    }

    //gets a photo by calling the model, with the same function-name.
    function get_photo($id)
    {
        $this->model_users->get_profile_photo($id);
    }

    //Makes 'logged_in_user' send a friend-request to tagget id
    function send_friend_request($id)
    {
        $myid = $this->session->userdata('id');
        $this->model_friends->request_friend($myid,$id);
        redirect("socialbuddy/index", 'refresh');
    }

    //Approve friend-request by making a 'relationship', and the deletes the 'friend-request'-row.
    function accept_friend_request($id)
    {
        $myid = $this->session->userdata('id');
        $this->model_friends->accept_friend_request($myid,$id);
       
        redirect("socialbuddy/index", 'refresh');
    }

    //Deletes the friend-request.
    function deny_friend_request($id)
    {
        $myid = $this->session->userdata('id');
        $this->model_friends->delete_friend_request($myid,$id);
        redirect("socialbuddy/index", 'refresh');
    }

    ////Makes 'logged_in_user' delete a 'relationship' with tagget id
    function delete_friend($id)
    {
        $myid = $this->session->userdata('id');
        $this->model_friends->remove_friend($myid,$id);
        redirect("socialbuddy/index", 'refresh');
    }

    //Creates a table like 'index', only showing 'logged_in_user's friends.
    function friends()
    {
        $data['title'] = 'See all Friends';
        $data['main_content'] = 'usr/all_friends';
        $data['results'] = $this->model_friends->list_all_friends($this->session->userdata('id'));
        $this->load->vars($data);
        $this->load->view('includes/template');

    }

    

}
?>
